package com.hyphenate.easeui.utils;

import android.content.Context;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.e.library.http.EApi;
import com.hyphenate.easeui.R;
import com.hyphenate.easeui.EaseUI;
import com.hyphenate.easeui.EaseUI.EaseUserProfileProvider;
import com.hyphenate.easeui.domain.EaseUser;

public class EaseUserUtils {
    
    static EaseUserProfileProvider userProvider;
    
    static {
        userProvider = EaseUI.getInstance().getUserProfileProvider();
    }
    
    /**
     * get EaseUser according username
     * @param username
     * @return
     */
    public static EaseUser getUserInfo(String username){
        if(userProvider != null)
            return userProvider.getUser(username);
        
        return null;
    }
    
//    /**
//     * set user avatar
//     * @param username
//     */
//    public static void setUserAvatar(Context context, String username, ImageView imageView){
//    	EaseUser user = getUserInfo(username);
//        if(user != null && user.getAvatar() != null){
//            try {
//                int avatarResId = Integer.parseInt(user.getAvatar());
//                Glide.with(context).load(avatarResId).into(imageView);
//            } catch (Exception e) {
//                //use default avatar
//                Glide.with(context).load(user.getAvatar()).diskCacheStrategy(DiskCacheStrategy.ALL).placeholder(R.drawable.ease_default_avatar).into(imageView);
//            }
//        }else{
//            Glide.with(context).load(R.drawable.ease_default_avatar).into(imageView);
//        }
//    }
//
//    /**
//     * set user's nickname
//     */
//    public static void setUserNick(String username,TextView textView){
//        if(textView != null){
//        	EaseUser user = getUserInfo(username);
//        	if(user != null && user.getNick() != null){
//        		textView.setText(user.getNick());
//        	}else{
//        		textView.setText(username);
//        	}
//        }
//    }

    /**
     * set user avatar
     * @param avatar
     */
    public static void setUserAvatar(Context context, String avatar, ImageView imageView){
        if (imageView == null){
            return;
        }
        if (TextUtils.isEmpty(avatar)){
            Glide.with(context).fromResource().load(R.drawable.ease_default_avatar).into(imageView);
            return;
        }
        String url = url(avatar);
        Glide.with(context).load(url).diskCacheStrategy(DiskCacheStrategy.ALL).error(R.drawable.ease_default_avatar).into(imageView);

    }

    /**
     * set user's nickname
     */
    public static void setUserNick(String username,TextView textView){
        if (textView == null){
            return;
        }
        textView.setText(username);
    }

    public static String url(String originUrl){
        if(TextUtils.isEmpty(originUrl)) {
            return originUrl;
        }

        if(!originUrl.startsWith("http")) {
            StringBuilder url = new StringBuilder(EApi.BASE_URL);
            url.append(originUrl);
            return url.toString();
        }
        return originUrl;
    }
    
}
